﻿// Original author contact info: Owen Emlen (owene_1998@yahoo.com)
// Note: other individuals may also have contributed to this code
// Project hosted on CodePlex.com as of 1/10/2009 at http://wpfimageviewer.codeplex.com
using System.ComponentModel;
using ImageAppDataModels;
using BrainTechLLC.FilterExtensions;
using BrainTechLLC;
using System;
using System.Reflection;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Windows;
using BrainTechLLC.WebCamLib3;

namespace WebCam
{
    public class LocalVideoDevicesViewModel : ObservableCollection<LocalVideoDevice>
    {
        protected LocalVideoDevice _selected;

        public LocalVideoDevice Selected
        {
            get
            {
                return _selected;
            }
            set
            {
                if (_selected != value)
                {
                    _selected = value;
                    OnPropertyChanged(new PropertyChangedEventArgs("Selected"));
                    OnPropertyChanged(new PropertyChangedEventArgs("SelectedName"));
                }
            }
        }

        public string SelectedName
        {
            get
            {
                return _selected == null ? string.Empty :
                    _selected.DeviceName;
            }
            set
            {
                if (_selected != null)
                {
                    if (_selected.DeviceName == value)
                        return;
                }

                for (int n = 0; n < this.Count; n++)
                {
                    if (this[n].DeviceName == value)
                    {
                        Selected = this[n];
                        break;
                    }
                }
            }
        }

        public LocalVideoDevicesViewModel(LocalVideoDevice selected)
        {
            var results = DeviceInfoFactory.GetAvailableCaptureDevices(null);

            this.Add(new LocalVideoDevice(string.Empty, string.Empty));
            this.Add(new LocalVideoDevice("Remote Feed", "Remote Feed"));

            foreach (var result in results)
            {
                this.Add(result);
            }

            _selected = selected;
        }
    }
}